//
//  GDBusStationLocationController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/5.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDBusStationLocationController.h"
#import "MBProgressHUD+MJ.h"
#import "GDBusStationLocationModel.h"
#import "GDTansitionModelDateOperation.h"
#import "GDBusStatsViewController.h"
@interface GDBusStationLocationController () <UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSArray * modelArray;
@end

@implementation GDBusStationLocationController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.backgroundColor = [UIColor whiteColor];
        [MBProgressHUD showMessage:@"正在查询,请稍后..." toView:self.view];
    [GDTansitionModelDateOperation initDateWithBUsStationLocation:self success:^(NSArray *model) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showSuccess:@"查询成功" toView:self.view];
        if (model.count != 0) {
            self.modelArray = model;
            [self updateUI:model];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 更新UI  转到别的控制器中更新UI
-(void) updateUI:(NSArray *) model{
    
    GDLog(@"%s 附近的站点有：%ld",__func__,(unsigned long)model.count);
    
    // 你身边的站点有：
    UILabel * locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(GDScreenSize.size.width * 0.15, GDScreenSize.size.height * 0.1, 200, 30)];
    locationLabel.text = @"你身边的站点有:";
    locationLabel.textColor = [UIColor redColor];
    [self.view addSubview:locationLabel];
    // 显示
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(GDScreenSize.size.width * 0.1, CGRectGetMaxY(locationLabel.frame)+ 30, GDScreenSize.size.width * 0.8, GDScreenSize.size.height * 0.4) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"cell";
    UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell   = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.backgroundColor = [UIColor clearColor];
    GDBusStationLocationModel * locationModel = self.modelArray[indexPath.row];
    cell.textLabel.text = locationModel.ID;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"跟你距离是:%@",locationModel.dist];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.modelArray.count * 20;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    GDBusStationLocationModel * locationModel = self.modelArray[indexPath.row];
    GDBusStatsViewController * statsViewController = [[GDBusStatsViewController alloc] init];
    statsViewController.statsName = locationModel.ID;
    statsViewController.cityName = self.cityName; // 临时值[NSString stringWithFormat:@"当前城市为:%@",
    [self.navigationController pushViewController:statsViewController animated:YES];
    
}
@end
